package com.hexus.eve.actions;

import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hexus.eve.entities.Account;
import com.hexus.eve.entities.security.User;
import com.hexus.eve.utils.CurrentUser;
import com.hexus.eve.utils.EmptyItems;
import com.opensymphony.xwork2.ActionSupport;

public class RefreshAccount extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Autowired
	SessionFactory sessionFactory;
	
	private Integer id = EmptyItems.INTEGER;
	private Account account;
	 
	
	public String execute() {
		if(id != -1) { // id is defined
			User user = CurrentUser.getUser();
			List<Account> accounts = user.getAccounts();
			for(Account a: accounts) {
				if(a.getId().intValue() == id) {
					account = a;
					a.updateAvailabilityStatus();
					a.synchronizeWithEve(sessionFactory);
					return SUCCESS;
				}
			}
			ResourceBundle bundle = ResourceBundle.getBundle ("global", getLocale());
			addActionError(bundle.getString("validation.cant.find.record"));
		}
		return SUCCESS;
	}

	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	
}
